<?php

/**
 * login actions.
 *
 * @package    Web20
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	public function executeIndex(sfWebRequest $request){
  		if ($request->isMethod('post')){
			if($token = $request->getParameter('token')){
                $auth_info = Rpx::getAuth($token);

			  	if ($auth_info['stat'] == 'ok') {
				    $profile = $auth_info['profile'];

					$interaction = InteractionTable::socialLogin($profile);

				    $this->getUser()->setAuthenticated(true);
				    $this->getUser()->setAttribute('interaction', $interaction);
				    $this->redirect('home/index');
			  	}
			  	else{
                    $this->getUser()->setFlash('error','Ocurrio el siguiente error: '.$auth_info['err']['msg']);
			  	}

			} else {
                $this->getUser()->setFlash('error','Ocurrio un error en el proceso.');
			}
		}
		elseif($this->getUser()->isAuthenticated()){
			$this->redirect('home/index');
		}
   		$form = new LoginForm();
   		$this->form = $form;
  	}

  	public function executeLogin(sfWebRequest $request){
  		$form = new LoginForm();
   		$this->form = $form;

  		if ($request->isMethod('post')){
  			$params['email']       = $request->getParameter('email');
  			$params['password']    = $request->getParameter('password');
            $params['_csrf_token'] = $request->getParameter('_csrf_token');

			$this->form->bind($params);
			if ($this->form->isValid()){
				if($user = Doctrine::getTable('User')->findOneBy('email', $params['email'])){
					if($user->validatePassword($params['password'])){
						$this->getUser()->setAuthenticated(true);
						$this->getUser()->setAttribute('user', $user);
	                    $this->getUser()->setFlash('success','Bienvenido! '.$user->getFirstName());

	                    if($site = $user->getSite()){
	                    	$this->redirect('site/index?name='.$site->getSlug());
	                    }
                    	$this->redirect('home/index');
					}
				}
			}
            $this->getUser()->setFlash('error','El usuario y/o clave ingresados son inválidos.');
		}

		$this->redirect('login/index');
 	}

    public function executeLogout(sfWebRequest $request){
        $this->getUser()->setAuthenticated(false);
        $this->getUser()->getAttributeHolder()->clear();
        $this->redirect('home/index');
    }

    public function executeSignup(sfWebRequest $request){
  		$form = new SignupForm();
   		$this->form = $form;

  		if ($request->isMethod('post')){
  			$params['email']       = $request->getParameter('email');
  			$params['password']    = $request->getParameter('password');
  			$params['repassword']  = $request->getParameter('repassword');
            $params['_csrf_token'] = $request->getParameter('_csrf_token');
			$this->form->bind($params);

			if ($this->form->isValid()){

				$user = new User();
				$user->setEmail($params['email']);
				$user->setPassword($params['password']);

				$user->save();
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->setAttribute('user', $user);
                $this->getUser()->setFlash('success','Bienvenido! El registro se realizó con éxito');

                $this->redirect('home/index');
			}

            $this->getUser()->setFlash('error','Ocurrio un error durante el registro.');
		}
    }



}
